package chen.huai.jie.spring.xmlconfig.mybatis;

import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.binding.MapperProxyFactory;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AutoMapperProxyFactory<T> extends MapperProxyFactory<T> {

    private final Map<Method, AutoMapperMethod> methodCache = new ConcurrentHashMap();

    public AutoMapperProxyFactory(Class<T> mapperInterface) {
        super(mapperInterface);
    }

    protected T newInstance(MapperProxy<T> mapperProxy) {
        return null;
    }

    private T newInstance(AutoMapperProxy<T> mapperProxy) {
        return (T) Proxy.newProxyInstance(this.getMapperInterface().getClassLoader(), new Class[]{this.getMapperInterface()}, mapperProxy);
    }

    public T newInstance(SqlSession sqlSession) {
        AutoMapperProxy<T> mapperProxy = new AutoMapperProxy(sqlSession, this.getMapperInterface(), this.methodCache);
        return this.newInstance(mapperProxy);
    }
}
